﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Web.UI;
using GTS.Channel.Api.Entity;
using Pub.Class;
using GTS.Channel.Api.Helper;

namespace GTS.Channel.Api {
    /// <summary>
    /// 通唐SMS和IVR的MO MT MR接收数据接口
    /// </summary>
    public class ToneTime : ISMSIVR {
        /// <summary>
        /// SPCode
        /// </summary>
        public string SPCode = "ToneTime";
        /// <summary>
        /// SMS MO的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMOData() {
            return null;
        }
        /// <summary>
        /// SMS MT的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMTData() {
            return null;
        }
        /// <summary>
        /// SMS MR的参数数据
        /// </summary>
        /// <returns></returns>
        public ChannelDealData GetSMSMRData() {
            //string data = "eyJzdGF0dXMiOjIwMCwidmFsdWUiOnsiY19jb250ZW50IjoiUCIsImNfY2hhbm5lbF9jb2RlIjoiMTUwNzMxMzA0MjEiLCJjX29wdGMiOiJDTUNDIiwiY19tb2JpbGUiOiIxMzkzMDAxODY1MyIsIm1fYW1vdW50IjoxLCJ0X2NvbGxlY3RfdGltZSI6IjIwMTItMDYtMjAgMTU6MjE6MTciLCJjX2xpbmtfaWQiOiI3MTgxMzIyMTA4NzI4NDk3OTk0NyJ9LCJhY3QiOiJhYyJ9";
            string data = Request2.GetRequestInputStream();
            if (data.IsNullEmpty()) { Base.WriteLog("原字符串不能为空"); Msg.WriteEnd(ResultEnum.Error); }

            string json = data.FormBase64(Encoding.UTF8);
            if (json.IsNullEmpty()) { Base.WriteLog("JSON不能为空"); Msg.WriteEnd(ResultEnum.Error); }

            int status = json.GetJsonInt("status");
            if (status != 200) { Base.WriteLog("错误的原字符串：{0}\\r\\nJSON：{1}".FormatWith(data, json)); Msg.WriteEnd(ResultEnum.Error); }

            string c_content = json.GetJsonString("c_content");
            string c_channel_code = json.GetJsonString("c_channel_code");
            string c_optc = json.GetJsonString("c_optc");
            string c_mobile = json.GetJsonString("c_mobile");
            decimal m_amount = json.GetJsonDecimal("m_amount")*10;
            string t_collect_time = json.GetJsonString("t_collect_time");
            string c_link_id = json.GetJsonString("c_link_id");

            switch (c_optc) {
                case "CMCC": c_optc = "1"; break;
                case "CU": c_optc = "2"; break;
                default: c_optc = "0"; break;
            }

            //初始值
            ChannelDealData info = new ChannelDealData();
            info.SPCode = SPCode;
            info.IsHidden = false;
            info.DeviceCode = "GENERICDEVICE";

            //接收值
            info.MRDate = DateTime.Now;
            if (t_collect_time.IsDateTime()) info.MRDate = t_collect_time.ToDateTime();
            info.SMSKey = c_optc.ToInt(0);
            info.Price = (int)m_amount;
            info.Mobile = c_mobile;
            info.ReceiveQuery = c_content;
            info.LinkID = c_link_id;
            info.LongPhone = c_channel_code;
            info.ResponseStatus = 1;

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.ReceiveQuery.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            string[] infos = info.ReceiveQuery.Split('_');
            if (infos.Length >= 4) {
                info.MountCode = infos[1].ToInt();
                info.GameCode = infos[2].PadLeft(5, '0');
                info.ChargeCode = infos[3];
            } else {
                info.MountCode = 493;
                info.GameCode = "00999";
                info.ChargeCode = "9";
            }

            //如果数据库中存在价格以数据库里为主
            ChannelInfo extInfo = Base.GetChannelInfo(SPCode, info.LongPhone, info.ReceiveQuery);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }

            return info;
        }
        /// <summary>
        /// IVR的MR参数数据
        /// </summary>
        /// <returns></returns>
        public IVRDealData GetIVRMRData() {
            //string data = "eyJzdGF0dXMiOjIwMCwidmFsdWUiOnsiY19jb250ZW50IjoiUCIsImNfY2hhbm5lbF9jb2RlIjoiMTUwNzMxMzA0MjEiLCJjX29wdGMiOiJDTUNDIiwiY19tb2JpbGUiOiIxMzkzMDAxODY1MyIsIm1fYW1vdW50IjoxLCJ0X2NvbGxlY3RfdGltZSI6IjIwMTItMDYtMjAgMTU6MjE6MTciLCJjX2xpbmtfaWQiOiI3MTgxMzIyMTA4NzI4NDk3OTk0NyJ9LCJhY3QiOiJhYyJ9";
            string data = Request2.GetRequestInputStream();
            if (data.IsNullEmpty()) { Base.WriteLog("原字符串不能为空"); Msg.WriteEnd(ResultEnum.Error); }

            string json = data.FormBase64(Encoding.UTF8);
            if (json.IsNullEmpty()) { Base.WriteLog("JSON不能为空"); Msg.WriteEnd(ResultEnum.Error); }

            int status = json.GetJsonInt("status");
            if (status != 200) { Base.WriteLog("错误的原字符串：{0}\\r\\nJSON：{1}".FormatWith(data, json)); Msg.WriteEnd(ResultEnum.Error); }

            string c_content = json.GetJsonString("c_content");
            string c_channel_code = json.GetJsonString("c_channel_code");
            string c_optc = json.GetJsonString("c_optc");
            string c_mobile = json.GetJsonString("c_mobile");
            decimal m_amount = json.GetJsonDecimal("m_amount")*10;
            string t_collect_time = json.GetJsonString("t_collect_time");
            string c_link_id = json.GetJsonString("c_link_id");

            switch (c_optc) {
                case "CMCC": c_optc = "1"; break;
                case "CU": c_optc = "2"; break;
                default: c_optc = "0"; break;
            }

            //初始值
            IVRDealData info = new IVRDealData();
            info.SPCode = SPCode;

            //接收值
            if (t_collect_time.IsDateTime()) info.RequestDate = t_collect_time.ToDateTime();
            info.SMSKey = c_optc.ToInt(0);
            info.Price = (int)m_amount;
            info.Mobile = c_mobile;
            info.LinkID = c_link_id;
            info.LongPhone = c_channel_code;
            info.ResponseStatus = 1;

            if (info.Mobile.IsNullEmpty() || info.LinkID.IsNullEmpty() || info.LongPhone.IsNullEmpty()) return null;

            //如果数据库中存在价格以数据库里为主
            IVRChannelInfo extInfo = Base.GetIVRChannelInfo(SPCode, info.LongPhone);
            if (extInfo.IsNotNull() && extInfo.ChannelName.IsNotNull()) {
                info.Price = extInfo.Price.Value;
                info.SMSKey = extInfo.CstCode;
            }

            return info;
        }
        /// <summary>
        /// 输出消息
        /// </summary>
        /// <param name="res"></param>
        public void Output(ResultEnum res) {
            Msg.WriteEnd(res.ToString());
        }
    }
}

